Add gtk_application_get_actions_for_accel()
authorRyan Lortie <desrt@desrt.ca>
Sun, 3 Aug 2014 18:27:51 +0000 (20:27 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 3 Aug 2014 18:27:51 +0000 (20:27 +0200)
This counterpart to gtk_application_get_accels_for_action() lets you
find out if a particular accelerator has one or more actions associated
with it. This might be useful from an accelerator editor or plugin
system to prevent the the installation of conflicting accelerators.

https://bugzilla.gnome.org/show_bug.cgi?id=721367

docs/reference/gtk/gtk3-sections.txt
gtk/gtkapplication.c
gtk/gtkapplication.h

index b2a773adf143605c3e286e5703becacb1a298116..cf17004f3a858c7d9d9ca707cfabe707b673c511 100644 (file)
@@ -7415,6 +7415,7 @@ gtk_application_remove_accelerator
 gtk_application_list_action_descriptions
 gtk_application_get_accels_for_action
 gtk_application_set_accels_for_action
+gtk_application_get_actions_for_accel
 
 <SUBSECTION Standard>
 GTK_TYPE_APPLICATION
index 6df6d2158a275bb6cd762adab48c14d1d0cf874b..88e3efeb22f5260ced5f52048ed0a9b90eb7099f 100644 (file)
@@ -446,6 +446,13 @@ accels_get_accels_for_action (Accels      *accels,
   return result;
 }
 
+static const gchar * const *
+accels_get_actions_for_accel (Accels         *accels,
+                              const AccelKey *accel_key)
+{
+  return g_hash_table_lookup (accels->accel_to_actions, accel_key);
+}
+
 static void
 accels_init (Accels *accels)
 {
@@ -1691,6 +1698,74 @@ gtk_application_get_accels_for_action (GtkApplication *application,
   return accels;
 }
 
+/**
+ * gtk_application_get_actions_for_accel:
+ * @application: a #GtkApplication
+ * @accel: an accelerator that can be parsed by gtk_accelerator_parse()
+ *
+ * Returns the list of actions (possibly empty) that @accel maps to.
+ * Each item in the list is a detailed action name in the usual form.
+ *
+ * This might be useful to discover if an accel already exists in
+ * order to prevent installation of a conflicting accelerator (from
+ * an accelerator editor or a plugin system, for example). Note that
+ * having more than one action per accelerator may not be a bad thing
+ * and might make sense in cases where the actions never appear in the
+ * same context.
+ *
+ * In case there are no actions for a given accelerator, an empty array
+ * is returned.  %NULL is never returned.
+ *
+ * It is a programmer error to pass an invalid accelerator string.
+ * If you are unsure, check it with gtk_accelerator_parse() first.
+ *
+ * Returns: (transfer full): a %NULL-terminated array of actions for @accel
+ *
+ * Since: 3.14
+ */
+gchar **
+gtk_application_get_actions_for_accel (GtkApplication *application,
+                                       const gchar    *accel)
+{
+  const gchar * const *actions_and_targets;
+  gchar **detailed_actions;
+  AccelKey accel_key;
+  guint i, n;
+
+  g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL);
+  g_return_val_if_fail (accel != NULL, NULL);
+
+  gtk_accelerator_parse (accel, &accel_key.key, &accel_key.modifier);
+
+  if (accel_key.key == 0)
+    {
+      g_critical ("invalid accelerator string '%s'", accel);
+      g_return_val_if_fail (accel_key.key != 0, NULL);
+    }
+
+  actions_and_targets = accels_get_actions_for_accel (&application->priv->accels, &accel_key);
+  n = actions_and_targets ? g_strv_length ((gchar **) actions_and_targets) : 0;
+
+  detailed_actions = g_new0 (gchar *, n + 1);
+
+  for (i = 0; i < n; i++)
+    {
+      const gchar *action_and_target = actions_and_targets[i];
+      const gchar *sep;
+      GVariant *target;
+
+      sep = strrchr (action_and_target, '|');
+      target = g_variant_parse (NULL, action_and_target, sep, NULL, NULL);
+      detailed_actions[i] = g_action_print_detailed_name (sep + 1, target);
+      if (target)
+        g_variant_unref (target);
+    }
+
+  detailed_actions[n] = NULL;
+
+  return detailed_actions;
+}
+
 GtkActionMuxer *
 gtk_application_get_action_muxer (GtkApplication *application)
 {
index 6fe1ff388cbe78787b68acaa5b09d9097ac8403b..b55d4a71a0a804306e0dd1548335dd370bd294c1 100644 (file)
@@ -145,6 +145,10 @@ gchar **         gtk_application_list_action_descriptions        (GtkApplication
 GDK_AVAILABLE_IN_3_12
 gchar **         gtk_application_get_accels_for_action           (GtkApplication       *application,
                                                                   const gchar          *detailed_action_name);
+GDK_AVAILABLE_IN_3_14
+gchar **         gtk_application_get_actions_for_accel           (GtkApplication       *application,
+                                                                  const gchar          *accel);
+
 
 GDK_AVAILABLE_IN_3_12
 void             gtk_application_set_accels_for_action           (GtkApplication       *application,